/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePasswordDlg.java
 *
 * Created on May 12, 2010, 11:35:57 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ManagementBiz;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class ChangePasswordDlg extends javax.swing.JDialog {

    ManagementBiz manaBuz = new ManagementBiz();
    CampaignMainFrm main;

    public ChangePasswordDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    /**
     * @return the boolean value
     */
    public boolean valid() {
        String oldPass = new String(pwfOldPassword.getPassword());
        String newPass = new String(pwfNewPassword.getPassword());
        String confirmPass = new String(pwfConfirmNewPass.getPassword());
        if (!oldPass.equalsIgnoreCase("") && !newPass.equalsIgnoreCase("") && !confirmPass.equalsIgnoreCase("")) {
            return true;
        }
        return false;
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        lblOld = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        lblConfirmPass = new javax.swing.JLabel();
        pwfOldPassword = new javax.swing.JPasswordField();
        pwfNewPassword = new javax.swing.JPasswordField();
        pwfConfirmNewPass = new javax.swing.JPasswordField();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblUserName = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Password");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnlTitle.setPreferredSize(new java.awt.Dimension(400, 59));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 30)); // NOI18N
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/CHANGEPASSWORD.jpg"))); // NOI18N
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
        getContentPane().add(pnlTitle, gridBagConstraints);

        lblOld.setText("Old Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 0);
        getContentPane().add(lblOld, gridBagConstraints);

        lblNewPass.setText("New Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 0);
        getContentPane().add(lblNewPass, gridBagConstraints);

        lblConfirmPass.setText("Confirm New Password : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 29, 0);
        getContentPane().add(lblConfirmPass, gridBagConstraints);

        pwfOldPassword.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 105;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 10, 0);
        getContentPane().add(pwfOldPassword, gridBagConstraints);

        pwfNewPassword.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 105;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 10, 0);
        getContentPane().add(pwfNewPassword, gridBagConstraints);

        pwfConfirmNewPass.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 105;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 29, 0);
        getContentPane().add(pwfConfirmNewPass, gridBagConstraints);

        btnSave.setBackground(new java.awt.Color(51, 102, 255));
        btnSave.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Save Change");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(btnSave, gridBagConstraints);

        btnCancel.setBackground(new java.awt.Color(51, 102, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(0, 19, 10, 0);
        getContentPane().add(btnCancel, gridBagConstraints);

        lblUserName.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblUserName.setForeground(new java.awt.Color(0, 0, 153));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 23, 0);
        getContentPane().add(lblUserName, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed

        String oldPass = new String(pwfOldPassword.getPassword());
        String newPass = new String(pwfNewPassword.getPassword());
        String confirmPass = new String(pwfConfirmNewPass.getPassword());
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data fully!");
        } else if (manaBuz.storePassword(lblUserName.getText(), oldPass, newPass, confirmPass)) {
            JOptionPane.showMessageDialog(this, manaBuz.getLasterror());
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, manaBuz.getLasterror());
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChangePasswordDlg dialog = new ChangePasswordDlg(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblConfirmPass;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JLabel lblOld;
    private javax.swing.JLabel lblTitle;
    public javax.swing.JLabel lblUserName;
    private javax.swing.JPanel pnlTitle;
    public javax.swing.JPasswordField pwfConfirmNewPass;
    public javax.swing.JPasswordField pwfNewPassword;
    public javax.swing.JPasswordField pwfOldPassword;
    // End of variables declaration//GEN-END:variables
}
